<div class="layuimini-main" id="DeviceReport-search">


    <table class="layui-hide" id="DevicePage" lay-filter="DevicePageTableFilter"></table>
    <div class="layui-row layui-col-space15">
        <div class="layui-panel defcenter">
            <button type="submit" class="layui-btn  data-add-btn" id="Save"><i class="layui-icon">&#xe605;</i> 保存到本地</button>
            <button type="submit" class="layui-btn layui-btn-danger  data-delete-btn" id="btnEditClose"><i class="layui-icon">&#x1006;</i> 取消</button>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'upload', 'element', 'layer', 'faceAPI'], function () {

        var layer = layui.layer,
            $ = layui.$;

        var layer = layui.layer,
            table = layui.table;

        var faceAPI = layui.faceAPI;

        var parentIndex = layer.index;
        table.render({
            elem: '#DevicePage',
            //**************************异步请求开始***********************
            url: 'api/FaceDeviceNew/SearchDevice',
            method: 'post',
            headers: {
                // token: 'sasasas',
                Authorization: "Bearer " + faceAPI.GetJWTToken()
            },//增加Token验证头
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                if (res.errCode == 0) {
                    var data = res.content;
                    var ret = {
                        "code": res.errCode, //解析接口状态
                        "msg": res.error, //解析提示文本
                        "count": data.totalCount, //解析数据长度
                        "data": data.dataList //解析数据列表
                    };
                    return ret;

                } else {
                    /*layer.open({
                        title: '请求错误',
                        content: '(' + res.errCode + ')' + res.error
                    });  */

                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": 0, //解析数据长度
                        "data": [] //解析数据列表
                    };
                }

            },
            //**************************异步请求结束***********************

            //toolbar: '#toolbarDemo',
            //defaultToolbar: ['filter', 'exports', 'print', {
            //    title: '提示',
            //    layEvent: 'LAYTABLE_TIPS',
            //    icon: 'layui-icon-tips'
            //}],
            cols: [[
                { type: "checkbox", width: 50 },
                // { field: 'id', width: 70, title: 'ID', sort: true },
                { field: 'sn', width: 180, title: '设备SN', sort: true },
                { field: 'mac', width: 160, title: 'mac地址', sort: true },
                { field: 'ip', title: 'IP地址', width: 160, sort: true },
                { field: 'port', title: '端口', width: 160, sort: true },
                {
                    field: 'isAdd', title: '状态', width: 160, sort: true,
                    templet: function (d) {
                        if (d.isAdd == true)
                            return '<span style="color:blue" > 已添加</span>';
                        else
                            return '<span style="color:red" > 未添加</span>';
                    }
                }
            ]],
            limits: [100, 200, 300, 400, 500, 600],
            limit: 100,
            page: true,
            skin: 'line'
        });

        $("#btnEditClose").click(function () {
            layer.close(parentIndex);
        });

        $("#Save").click(function () {
            var checkStatus = table.checkStatus('DevicePage');
            var data = checkStatus.data;
            for (var i = 0; i < data.length; i++) {
                if (data[i].isAdd == true) {
                    layer.alert(data[i].sn + ":已经添加", { icon: 2 });
                    return;
                }
            }
            faceAPI.Ajax({
                type: 'Post',
                data: { Devices: data },
                url: '/api/FaceDeviceNew/AddDevices',
                success: function (result) {
               
                    if (result.result == false) {
                        layer.alert(result.error, { icon: 2 });
                    }
                    else {
                        var message = "";
                        for (var i = 0; i < result.content.length; i++) {
                            if (result.content[i].result == false) {
                                message += result.content[i].content + ":添加失败," + result.content[i].error;
                            } else {
                                message += result.content[i].content + ":添加成功,"
                            }                          
                        }

                        layer.close(parentIndex);
                    }
                    // layer.close(index);
                }
            });


        });
    });

</script>
